/*
 * Copyright 2010 Biz-e (info@biz-e.nl)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.site.client.editor;

import nl.biz_e.gwt.baselib.ui.client.widgets.RichTextEditor;
import nl.biz_e.site.client.ContentEditor;
import nl.biz_e.site.client.ContentService;
import nl.biz_e.site.client.ContentServiceAsync;
import nl.biz_e.site.shared.models.Content;
import nl.biz_e.site.shared.models.Version;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Grid;

/**
 * Main panel for the {@link ContentEditor} application
 * 
 * @author Joost A. Bloemsma
 */
public class EditorPanel extends EditorPanelBase {
    private final ContentServiceAsync contentService;
    private final ContentSelector selector;
    private final RichTextEditor editor;

    /**
     * Constructs a new EditorPanel instance
     */
    public EditorPanel() {
        super();
        contentService = GWT.create(ContentService.class);
        Grid grid = new Grid(2, 1);
        grid.setWidth("100%");
        selector = new ContentSelector(this);
        grid.setWidget(0, 0, selector);
        editor = new RichTextEditor() {
            @Override
            protected void onResize() {
                int height = (Window.getClientHeight() - getDeckTop()) - 100;
                if (height < 10)
                    height = 10;
                setDeckHeight(height);
            }
        };
        grid.setWidget(1, 0, editor);
        this.add(grid);
    }

    ContentServiceAsync getService() {
        return contentService;
    }

    /**
     * @param version
     */
    public void versionSelected(Version version) {
        contentService.getContent(version, new AsyncCallback<Content>() {
            @Override
            public void onSuccess(Content result) {
                editor.setHTML(result.getText());
            }

            @Override
            public void onFailure(Throwable caught) {
                getAppManager().showMessage(
                        getAppManager().getMessages().serverError());
            }
        });
    }

    /**
     * Saves the current content as a new version
     * 
     * @param oldVersion
     *            The current version information
     */
    public void saveNewContent(Version oldVersion) {
        Content c = new Content();
        c.setText(editor.getHTML());
        contentService.saveContent(c, oldVersion.getKey(),
                new AsyncCallback<Version>() {

                    @Override
                    public void onFailure(Throwable caught) {
                        getAppManager().showMessage(
                                getAppManager().getMessages().serverError());
                    }

                    @Override
                    public void onSuccess(Version result) {
                        selector.selectVersion(result);
                    }
                });
    }
}
